package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.beans.TrafficUvCt;
import com.atguigu.gmall.publisher.service.TrafficStatsService;
import com.atguigu.gmall.publisher.utils.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Felix
 * @date 2023/7/21
 * 流量域统计controller
 */
@RestController
public class TrafficStatsController {

    @Autowired
    private TrafficStatsService trafficStatsService;

    @RequestMapping("/ch")
    public String getChUvCt(
        @RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TrafficUvCt> uvCtList = trafficStatsService.getChUvCt(date, limit);
        List chList = new ArrayList();
        List ctList = new ArrayList();
        for (TrafficUvCt uvCt : uvCtList) {
            chList.add(uvCt.getCh());
            ctList.add(uvCt.getUvCt());
        }

        String json = "{\n" +
            "  \"status\": 0,\n" +
            "  \"data\": {\n" +
            "    \"categories\": [\"" + StringUtils.join(chList, "\",\"") + "\"],\n" +
            "    \"series\": [\n" +
            "      {\n" +
            "        \"name\": \"渠道\",\n" +
            "        \"data\": ["+StringUtils.join(ctList,",")+"]\n" +
            "      }\n" +
            "    ]\n" +
            "  }\n" +
            "}";
        return json;
    }
}
